﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SustainMagicCard : AbstractMagicCard {
    public GameObject SustainMagic;
    public BeamRay beamRay;
    public VRControllerInteractive controllerInteractive;
    public float power;

    private void Start()
    {
        controllerInteractive.StartHolding += HoldingControllerAddAction;
        controllerInteractive.ReleaseHolding += HoldingControllerReleaseAction;
    }

    private void Update()
    {
        if (state == CardState.Charged && controllerInteractive.isHolding)
        {
            beamRay.isShoot = true;
            transform.position = controllerInteractive.controller.transform.position;
            transform.rotation = controllerInteractive.controller.transform.rotation;
            if (SustainMagic.activeSelf)
            {
                power -= Time.deltaTime;
                if (power < 0)
                {
                    foreach(var child in transform.GetComponentsInChildren<Transform>())
                    {
                        Destroy(child.gameObject);
                    }
                    //Destroy(this.gameObject);
                    return;
                }
            }
        }
        else
        {
            beamRay.isShoot = false;
        }
    }



    private void PadClick(object sender, ClickedEventArgs e)
    {
        if(beamRay.isHit)
        {
            SustainMagic.SetActive(true);
        }
        
    }

    private void PadUnClick(object sender, ClickedEventArgs e)
    {
        SustainMagic.SetActive(false);
    }

    protected override void HoldingControllerAddAction()
    {
        controllerInteractive.controller.PadClicked += PadClick;
        controllerInteractive.controller.PadUnclicked += PadUnClick;
    }

    protected override void HoldingControllerReleaseAction()
    {
        controllerInteractive.controller.PadClicked -= PadClick;
        controllerInteractive.controller.PadUnclicked -= PadUnClick;
    }
}
